<?php
class ControllerCatalogExtProduct extends Controller {
	private $error = array();
	public function index() {
		$this->load->language('catalog/product');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('catalog/ext_product');

		$this->getList();
	}
	public function insert() {
		$this->load->language('catalog/product');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('catalog/ext_product');

		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$product_id = $this->model_catalog_ext_product->addProduct($this->request->post);

			$this->oplog->add_log(1);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}



			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('catalog/ext_product&token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}
	public function update() {
		$this->load->language('catalog/product');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('catalog/ext_product');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {
			$this->model_catalog_ext_product->editProduct($this->request->get['product_id'], $this->request->post);

			$this->oplog->add_log(2);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}


			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('catalog/ext_product', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}
	public function delete() {
		$this->load->language('catalog/product');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('catalog/product');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $product_id) {
				$this->model_catalog_product->deleteProduct($product_id);
			}

			$this->oplog->add_log(3);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_model'])) {
				$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_price'])) {
				$url .= '&filter_price=' . $this->request->get['filter_price'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
			}

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		}
		else {
			$filter_name = null;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		}
		else {
			$filter_status = null;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		}
		else {
			$sort = 'pd.name';
		}


		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		}
		else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		}
		else {
			$page = 1;
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		//filter category start//
		if (isset($this->request->get['filter_category'])) {
			$url .= '&filter_category=' . $this->request->get['filter_category'];
		}
		//filter category end//


		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'), 
			'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'), 
			'separator' => false 
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'), 
			'href' => $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, 'SSL'), 
			'separator' => ' :: ' 
			);

			$this->data['insert'] = $this->url->link('catalog/ext_product/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');

			$this->data['delete'] = $this->url->link('catalog/ext_product/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

			$this->data['products'] = array();

			$data = array(
			'filter_name' => $filter_name, 
			'filter_status' => $filter_status, 
			'sort' => $sort, 

			'order' => $order, 
			'start' => ($page - 1) * $this->config->get('config_admin_limit'), 
			'limit' => $this->config->get('config_admin_limit') 
			);
			$product_total = $this->model_catalog_ext_product->getTotalProducts($data);

			$results = $this->model_catalog_ext_product->getProducts($data);

			foreach ($results as $result) {
				$action = array();
					
				$action[] = array(
				'text' => $this->language->get('text_edit'), 
				'href' => $this->url->link('catalog/ext_product/update', 'token=' . $this->session->data['token'] . '&product_id=' . $result['id'] . $url, 'SSL') 
				);
					

					
					
				$this->data['products'][] = array(
				'product_id' => $result['product_id'], 
				'name' => $result['name'], 
				'model' => $result['model'], 
				'price' => $result['price'], 
				'special' => $special, 
				//filter category start//
				'category' => $category, 
				//filter category end//
				'image' => $image, 
				'href' => $this->url->link('product/product', $url . '&product_id=' . $result['product_id']), 
				'quantity' => $result['quantity'], 
				'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')), 
				'selected' => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']), 
				'action' => $action, 
				'is_hot' => $result['is_hot'], 
				'is_rec' => $result['is_rec'] 
				);
			}

			$this->data['heading_title'] = $this->language->get('heading_title');

			$this->data['text_enabled'] = $this->language->get('text_enabled');
			$this->data['text_disabled'] = $this->language->get('text_disabled');
			$this->data['text_no_results'] = $this->language->get('text_no_results');
			$this->data['text_image_manager'] = $this->language->get('text_image_manager');

			$this->data['column_image'] = $this->language->get('column_image');
			$this->data['column_name'] = $this->language->get('column_name');
			$this->data['column_model'] = $this->language->get('column_model');
			$this->data['column_price'] = $this->language->get('column_price');
			// Add
			$this->data['column_category'] = $this->language->get('column_category');
			// End add
			$this->data['column_quantity'] = $this->language->get('column_quantity');
			$this->data['column_status'] = $this->language->get('column_status');
			$this->data['column_action'] = $this->language->get('column_action');

			$this->data['button_copy'] = $this->language->get('button_copy');
			$this->data['button_insert'] = $this->language->get('button_insert');
			$this->data['button_delete'] = $this->language->get('button_delete');
			$this->data['button_filter'] = $this->language->get('button_filter');

			$this->data['token'] = $this->session->data['token'];

			if (isset($this->error['warning'])) {
				$this->data['error_warning'] = $this->error['warning'];
			}
			else {
				$this->data['error_warning'] = '';
			}

			if (isset($this->session->data['success'])) {
				$this->data['success'] = $this->session->data['success'];
					
				unset($this->session->data['success']);
			}
			else {
				$this->data['success'] = '';
			}

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_model'])) {
				$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_price'])) {
				$url .= '&filter_price=' . $this->request->get['filter_price'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
			}

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if ($order == 'ASC') {
				$url .= '&order=DESC';
			}
			else {
				$url .= '&order=ASC';
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->data['sort_name'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, 'SSL');
			$this->data['sort_model'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.model' . $url, 'SSL');
			//filter category start//
			$this->data['sort_category'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p2c.category' . $url, 'SSL');
			//filter category end//
			$this->data['sort_price'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.price' . $url, 'SSL');
			$this->data['sort_quantity'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.quantity' . $url, 'SSL');
			$this->data['sort_status'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.status' . $url, 'SSL');
			$this->data['sort_order'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, 'SSL');

			$url = '';

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}

			if (isset($this->request->get['filter_model'])) {
				$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
			}
			// Add
			if (isset($this->request->get['filter_category'])) {
				$url .= '&filter_category=' . $this->request->get['filter_category'];
			}
			// End add
			if (isset($this->request->get['filter_price'])) {
				$url .= '&filter_price=' . $this->request->get['filter_price'];
			}

			if (isset($this->request->get['filter_quantity'])) {
				$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
			}

			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			$pagination = new Pagination();
			$pagination->total = $product_total;
			$pagination->page = $page;
			$pagination->limit = $this->config->get('config_admin_limit');
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

			$this->data['pagination'] = $pagination->render();

			$this->data['filter_name'] = $filter_name;
			// Add
			$this->data['filter_category'] = $filter_category;
			// End add
			$this->data['filter_model'] = $filter_model;
			$this->data['filter_price'] = $filter_price;
			$this->data['filter_quantity'] = $filter_quantity;
			$this->data['filter_status'] = $filter_status;

			$this->data['sort'] = $sort;
			$this->data['order'] = $order;

			$this->template = 'catalog/ext_product.tpl';
			$this->children = array(
			'common/header', 
			'common/footer' 
			);

			$this->response->setOutput($this->render());
	}
	private function getForm() {

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_select_all'] = $this->language->get('text_select_all');
		$this->data['text_unselect_all'] = $this->language->get('text_unselect_all');
		$this->data['text_plus'] = $this->language->get('text_plus');
		$this->data['text_minus'] = $this->language->get('text_minus');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_option'] = $this->language->get('text_option');
		$this->data['text_option_value'] = $this->language->get('text_option_value');
		$this->data['text_select'] = $this->language->get('text_select');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_percent'] = $this->language->get('text_percent');
		$this->data['text_amount'] = $this->language->get('text_amount');


		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_attribute'] = $this->language->get('button_add_attribute');
		$this->data['button_add_option'] = $this->language->get('button_add_option');
		$this->data['button_add_option_value'] = $this->language->get('button_add_option_value');
		$this->data['button_add_discount'] = $this->language->get('button_add_discount');
		$this->data['button_add_special'] = $this->language->get('button_add_special');
		$this->data['button_add_image'] = $this->language->get('button_add_image');
		$this->data['button_remove'] = $this->language->get('button_remove');

		$this->data['tab_general'] = $this->language->get('tab_general');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		}
		else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		}
		else {
			$this->data['error_name'] = array();
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		/*取所有的父分类*/
		$this->data['parents']=$this->model_catalog_ext_product->getProByPid(0);
		if($this->request->get['product_id']){
			$this->data['product']=$this->model_catalog_ext_product->getProById($this->request->get['product_id']);
		}

		//filter category end//
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'), 
			'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'), 
			'separator' => false 
		);

		$this->data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'), 
			'href' => $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, 'SSL'), 
			'separator' => ' :: ' 
			);

			$this->data['token'] = $this->session->data['token'];
			if (!isset($this->request->get['product_id'])) {
				$this->data['action'] = $this->url->link('catalog/ext_product/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
			}
			else {
				$this->data['action'] = $this->url->link('catalog/ext_product/update', 'token=' . $this->session->data['token'] . '&product_id=' . $this->request->get['product_id'] . $url, 'SSL');
			}

			if (isset($this->request->post['status'])) {
				$this->data['status'] = $this->request->post['status'];
			}
			elseif (!empty($product_info)) {
				$this->data['status'] = $product_info['status'];
			}
			else {
				$this->data['status'] = 1;
			}
			//取产品品牌
			$this->load->model('catalog/manufacturer');
			$this->data['manufacturers']=$this->model_catalog_manufacturer->getManufacturers();



			$this->template = 'catalog/ext_product_form.tpl';
			$this->children = array(
			'common/header', 
			'common/footer' 
			);

			$this->response->setOutput($this->render());
	}
	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'catalog/ext_product')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}


		if (!$this->error) {
			return true;
		}
		else {
			return false;
		}
	}
	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'catalog/product')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->error) {
			return true;
		}
		else {
			return false;
		}
	}

	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_model']) || isset($this->request->get['filter_category_id'])) {
			$this->load->model('catalog/product');

			if (isset($this->request->get['filter_name'])) {
				$filter_name = $this->request->get['filter_name'];
			}
			else {
				$filter_name = '';
			}

			if (isset($this->request->get['filter_model'])) {
				$filter_model = $this->request->get['filter_model'];
			}
			else {
				$filter_model = '';
			}

			if (isset($this->request->get['filter_category_id'])) {
				$filter_category_id = $this->request->get['filter_category_id'];
			}
			else {
				$filter_category_id = '';
			}

			if (isset($this->request->get['filter_sub_category'])) {
				$filter_sub_category = $this->request->get['filter_sub_category'];
			}
			else {
				$filter_sub_category = '';
			}

			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];
			}
			else {
				$limit = 20;
			}

			$data = array(
				'filter_name' => $filter_name, 
				'filter_model' => $filter_model, 
				'filter_category' => $filter_category_id, 
				'filter_sub_category' => $filter_sub_category, 
				'start' => 0, 
				'limit' => $limit 
			);

			$results = $this->model_catalog_product->getProducts($data);

			foreach ($results as $result) {
				$option_data = array();

				$product_options = $this->model_catalog_product->getProductOptions($result['product_id']);

				foreach ($product_options as $product_option) {
					if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
						$option_value_data = array();

						foreach ($product_option['product_option_value'] as $product_option_value) {
							$option_value_data[] = array(
								'product_option_value_id' => $product_option_value['product_option_value_id'], 
								'option_value_id' => $product_option_value['option_value_id'], 
								'name' => $product_option_value['name'], 
								'price' => (float) $product_option_value['price'] ? $this->currency->format($product_option_value['price'], $this->config->get('config_currency')) : false, 
								'price_prefix' => $product_option_value['price_prefix'] 
							);
						}

						$option_data[] = array(
							'product_option_id' => $product_option['product_option_id'], 
							'option_id' => $product_option['option_id'], 
							'name' => $product_option['name'], 
							'type' => $product_option['type'], 
							'option_value' => $option_value_data, 
							'required' => $product_option['required'] 
						);
					}
					else {
						$option_data[] = array(
							'product_option_id' => $product_option['product_option_id'], 
							'option_id' => $product_option['option_id'], 
							'name' => $product_option['name'], 
							'type' => $product_option['type'], 
							'option_value' => $product_option['option_value'], 
							'required' => $product_option['required'] 
						);
					}
				}

				$json[] = array(
					'product_id' => $result['product_id'], 
					'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')), 
					'model' => $result['model'], 
					'option' => $option_data, 
					'price' => $result['price'] 
				);
			}
		}

		$this->response->setOutput(json_encode($json));
	}

	/**
	 * 修改产品状态（热销、推荐）
	 */
	function status() {
		$this->load->model('catalog/product');
		$json = $this->model_catalog_product->changeStatus($this->request->get);
		$this->response->setOutput($json);
	}

	public function getProByPid($id){
		if($this->request->get['pid']){
			$id=$this->request->get['pid'];
		}
		$this->load->model('catalog/ext_product');
		$json = json_encode($this->model_catalog_ext_product->getProByPid($id));
		$this->response->setOutput($json);
	}

	/**
	 * 增加产品与商品关系
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function addProductRelated($data){
		if($this->request->get['pid']){
			$data['pid']=$this->request->get['pid'];
		}
		if($this->request->get['eid']){
			$data['eid']=$this->request->get['eid'];
		}
		if($this->request->get['parent_id']){
			$data['parent_id']=$this->request->get['parent_id'];
		}
		if($this->request->get['mid']){
			$data['mid']=$this->request->get['mid'];
		}
		$this->load->model('catalog/ext_product');
		$json = json_encode($this->model_catalog_ext_product->addProductRelated($data));
		$this->response->setOutput($json);
	}

	public function delProductRelated($data){
		if($this->request->get['pid']){
			$data['pid']=$this->request->get['pid'];
		}
		if($this->request->get['eid']){
			$data['eid']=$this->request->get['eid'];
		}
		$this->load->model('catalog/ext_product');
		$json = json_encode($this->model_catalog_ext_product->delProductRelated($data));
		$this->response->setOutput($json);
	}

	public function getExtProductByMid($mid){
		if($this->request->get['mid']){
			$mid=$this->request->get['mid'];
		}
		$this->load->model('catalog/ext_product');
		$json = json_encode($this->model_catalog_ext_product->getExtProductByMid($mid));
		$this->response->setOutput($json);
	}


}
?>